*===============================================================================
* Project title		Savings Accounts to Borrow Less	
* PIs			Felipe Kast, Dina Pomeranz 
* File name		4a_round_amounts.do
*-------------------------------------------------------------------------------
* Description		Creates probability of reporting round amounts table  
* Outputs		Table A16
* Spaces per tab 	8 
*===============================================================================

*===============================================================================
*TOC
*1)	Prepare data for analysis
*2)	Creating new variables for analysis
*3)	Regression 
*4)	Write table 
*===============================================================================

*===============================================================================
* Section 1 - Prepare data for analysis
*===============================================================================

clear all
global path "~/file_server/project_savings/2_shared/impact/do/November2019"	// Path to processed data
global results "$path/results"		
cd "$results"									// Change to directory to store results 
set more off

*Loading processed data
	use $path/datasets/impactDatabase.dta, clear


*===============================================================================
* Section 2 - Creating new variables for analysis
*===============================================================================

*-------------------------------
* Creating a variable for the post period that does not include FE balances 
gen allSavings_nonFE = allSavings								// Data is at the household-year level, baseline data already excludes FE balances
egen temprowtotal = rowtotal(d1_bm d1_cm d1_dm d1_em d1_fm d1_gm d1_hm d1_im), missing 		// This variable excludes FE balances 
replace allSavings_nonFE = temprowtotal if post == 1						// Replacing all post period total savings to create the correct variable for rounding analysis
drop temprowtotal 

* 2.1 For each savings component.
*-------------------------------

foreach x in a b c d e f g h i {

	generate rounded1_d1_`x'm = ((int(d1_`x'm/10000 )*10000)  == d1_`x'm) if d1_`x'm != .  

	*Assume a balance of zero is not a round amount 

	replace rounded1_d1_`x'm = 0 if d1_`x'm == 0

}


* 2.2 Creating round variable for the whole of savings variable, excluding account balances 
*--------------------------------------------------------------------------

generate rounded1_allSavings = ((int(allSavings_nonFE/10000)*10000) == allSavings_nonFE ) if allSavings_nonFE != . 

replace rounded1_allSavings = 0 if allSavings_nonFE == 0


*2.3 Generate a variable that is marking all those ids that report savings > median
*in the PRE period, for each savings component
*-----------------------------------------------------------------------------------
foreach x in  c d e f g h i  {   
sum  d1_`x'm , d
local med = `r(p50)'
local top75 = `r(p75)'
di "`med'"
di "`top75'"
*above median indicator
gen abv_med_d1_`x'm = 1 if d1_`x'm > `med' & d1_`x'm !=. & post == 0
bysort idBaseSurvey (abv_med_d1_`x'm): replace abv_med_d1_`x'm = abv_med_d1_`x'm[1]   
*above p75 indicator
gen abv_p75_d1_`x'm = 1 if d1_`x'm > `top75' & d1_`x'm !=. & post == 0
bysort idBaseSurvey (abv_p75_d1_`x'm): replace abv_p75_d1_`x'm = abv_p75_d1_`x'm[1]
}

sort idBaseSurvey year

* 2.4 Generate a variable that marks all the ids that report 
* savings > median, for total savings 
*-------------------------------------------------------------------------------
sum allSavings_nonFE, d // we use the overall savings to compute the median and percentiles which is why the number of observations is different in the pre and post samples 
local med= `r(p50)'
local top75= `r(p75)'
local top90= `r(p90)'
local top95= `r(p95)'
local top99= `r(p99)'
di "`med'"
di "`top75'"
di "`top90'"
*above median indicator
gen abv_med_allSav = 1 if allSavings_nonFE > `med' & allSavings_nonFE !=. & post == 0
bysort idBaseSurvey (abv_med_allSav): replace abv_med_allSav = abv_med_allSav[1]  
*below median indicator
gen bel_med_allSav = 1 if allSavings_nonFE <= `med' & allSavings_nonFE !=. & post == 0
bysort idBaseSurvey (bel_med_allSav): replace bel_med_allSav = bel_med_allSav[1]   
*above p75 indicator
gen abv_p75_allSav = 1 if allSavings_nonFE> `top75' & allSavings_nonFE !=. & post == 0
bysort idBaseSurvey (abv_p75_allSav): replace abv_p75_allSav = abv_p75_allSav[1]
*above p90 indicator
gen abv_p90_allSav = 1 if allSavings_nonFE> `top90' & allSavings_nonFE !=. & post == 0
bysort idBaseSurvey (abv_p90_allSav): replace abv_p90_allSav = abv_p90_allSav[1]
*above p95 indicator
gen abv_p95_allSav = 1 if allSavings_nonFE> `top95' & allSavings_nonFE !=. & post == 0
bysort idBaseSurvey (abv_p95_allSav): replace abv_p95_allSav = abv_p95_allSav[1]
*above p99 indicator
gen abv_p99_allSav = 1 if allSavings_nonFE> `top99' & allSavings_nonFE !=. & post == 0
bysort idBaseSurvey (abv_p99_allSav): replace abv_p99_allSav = abv_p99_allSav[1]


sort idBaseSurvey year

*===============================================================================
* Section 3 - Regression for rounded 1
*===============================================================================
local i = 1

foreach y in rounded1_allSavings {
           
        reg d.`y'
        cap drop t1        
        egen t1 = max(e(sample)), by(idBaseSurvey)
        reghdfe `y' 1.accountAccess#1.post post if t1==1,  ///
        abs(i.idBaseSurvey) vce(cluster groupId)     
        
        distinct idBaseSurvey  if e(sample)
        estadd scalar num_ind = r(ndistinct)
        estadd local ind_fixeff = "Yes"
        estadd local stratum_post_fe = "Yes"
        estadd local samp = "Full"
	estadd local amt = ""
        eststo E`i'A
            
        di in red "*Above median in PRE"
        reghdfe `y' 1.accountAccess#1.post post if t1==1 &  abv_med_allSav == 1, ///
        abs(i.idBaseSurvey) vce(cluster groupId) 
        distinct idBaseSurvey  if e(sample)
        estadd scalar num_ind = r(ndistinct)
        estadd local ind_fixeff = "Yes"
        estadd local stratum_post_fe = "Yes"
        estadd local samp = "Above P50 "
	estadd local amt = "`med'"
        eststo E`i'C      
    
        di in red "*Above P75 in PRE"
        reghdfe `y' 1.accountAccess#1.post post if t1==1 &  abv_p75_allSav == 1, ///
        abs(i.idBaseSurvey) vce(cluster groupId)

        distinct idBaseSurvey  if e(sample)
        estadd scalar num_ind = r(ndistinct)
        estadd local ind_fixeff = "Yes"
        estadd local stratum_post_fe = "Yes"
        estadd local samp = "Above P75"
	estadd local amt = "`top75'"
        eststo E`i'D      
 
        di in red "*Above P90 in PRE"
        reghdfe `y' 1.accountAccess#1.post post if t1==1 &  abv_p90_allSav == 1, ///
        abs(i.idBaseSurvey) vce(cluster groupId) 

        distinct idBaseSurvey  if e(sample)
        estadd scalar num_ind = r(ndistinct)
        estadd local ind_fixeff = "Yes"
        estadd local stratum_post_fe = "Yes"
        estadd local samp = "Above P90 "
	estadd local amt = "`top90'"
        eststo E`i'E    
	
	di in red "*Above P95 in PRE"
        reghdfe `y' 1.accountAccess#1.post post if t1==1 &  abv_p95_allSav == 1, ///
        abs(i.idBaseSurvey) vce(cluster groupId) 

        distinct idBaseSurvey  if e(sample)
        estadd scalar num_ind = r(ndistinct)
        estadd local ind_fixeff = "Yes"
        estadd local stratum_post_fe = "Yes"
        estadd local samp = "Above P95"
	estadd local amt = "`top95'"
        eststo E`i'F 

}

*===============================================================================
* Section 4 - Write table 
*===============================================================================
esttab E1* using table_rounding.tex,                                                 			/// 
        replace cells(b(star fmt(%12.3fc))                                                              ///
        se(par fmt(%12.3fc))) label style(tex)                                                          ///
        stats(ind_fixeff num_ind N samp, fmt( %12.0fc %12.0fc %12.0fc %12.0fc %0s %12.0fc)  		///
        label("\hline Individual FE" "Individuals" "Observations" "Sample"))				///
        mlabels(,none) collabels(, none) eqlabels(, none) noobs                                         ///
        drop(post _cons) varlabels(1.accountAccess#1.post "Account $\times$ post")                      ///
        starlevels(* 0.1 ** 0.05 *** 0.01)                                                              ///
        prehead(\begin{tabular}{l*{@M}{c}}  \hline \hline )    						///
        prefoot( ) postfoot(\hline \hline \end{tabular})  
	
